﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OOP_DinhCongThuat_K56_Lan1
{
    class MyDate
    {
        private int day;
        private int month;
        private int year;

        const int DAY = 1;
        const int MONTH = 2;
        const int YEAR = 3;

        //ham kiem tra ngay
        private Boolean checkDay (int day)
        {
            //neu ngay nho hon 0 thi tra ve gia thi false
            if (day < 0) return false;

            //kiem tra xem ngay nhap vao co lon hon gia tri toi da voi thang va nam khong neu dung thi temp nhan gai tri 0 sai temp nhan gia tri 1
            int temp = 1;
            switch (this.month)
            {
                case 1:
                    {
                        if (day > 31) temp = 0;
                        break;
                    }
                case 2:
                    {
                        if (this.year % 4 == 0)
                        {
                            if (day > 29) temp = 0;
                            break;
                        }
                        else
                        {
                            if (day > 28) temp = 0;
                            break;
                        }
                    }
                case 3:
                    {
                        if (day > 31) temp = 0;
                        break;
                    }
                case 4:
                    {
                        if (day > 30) temp = 0;
                        break;
                    }
                case 5:
                    {
                        if (day > 31) temp = 0;
                        break;
                    }
                case 6:
                    {
                        if (day > 30) temp = 0;
                        break;
                    }
                case 7:
                    {
                        if (day > 31) temp = 0;
                        break;
                    }
                case 8:
                    {
                        if (day > 31) temp = 0;
                        break;
                    }
                case 9:
                    {
                        if (day > 30) temp = 0;
                        break;
                    }
                case 10:
                    {
                        if (day > 31) temp = 0;
                        break;
                    }
                case 11:
                    {
                        if (day > 30) temp = 0;
                        break;
                    }
                case 12:
                    {
                        if (day > 31) temp = 0;
                        break;
                    }
            }
            // tem nhan gai tri 0 thi tra ve false
            if (temp == 0) return false;

            //neu ngay nhap vao phu hop thi tra ve gia tri true
            return true;
        }

        //ham kiem tra thang
        private Boolean checkMonth(int month)
        {
            if (month > 12) return false;
            if (month < 0) return false;
            return true;
        }

        //ham kiem tra nam
        private Boolean checkYear(int year)
        {
            if (year < 0) return false;
            else return true;
        }


        //ham nhap "set()"
        public void set(int field, int value)
        {
            //neu field = DAY va kiem tra ngay phu hop thi gan gia tri cho bien "day"
            if (field == DAY)
            {
                if (checkDay(value) == true) this.day = value;
            }

            //neu file = MONTH va kiem tra thang phu hop thi gia gia tri cho bien "month"
            if (field == MONTH)
            {
                if (checkMonth(value) == true) this.month = value;
            }

            //neu field = year va kiem tra nam phu hop thi gan gai tri cho bien "year"
            if (field == YEAR)
            {
                if (checkYear(value) == true)this.year = value; 
            }
        }

        //ham tra ve thong tin "get()"
        public int get (int field)
        {
            if (field == DAY) return this.day;
            if (field == MONTH) return this.month;
            if (field == YEAR) return this.year;
            return 0;
        }

        //phuong thuc khoi tao gan ngay, thang, nam hien tai
        public MyDate()
        {
            System.DateTime date = System.DateTime.Now;
            day = date.Day;
            month = date.Month;
            year = date.Year;
        }

        //phuong thuc khoi tao voi tham gia truyen vao la ngay, thang, nam
        public MyDate(int day, int month, int year)
        {
            if (checkDay(day) == true) this.day = day;
            if(checkMonth(month) == true) this.month = month;
            if (checkYear(year) == true) this.year = year;
        }

        //phuong thuc khoi tao co tham so truyen vao la 1 doi tuong cua lop MyDate
        public MyDate(MyDate date)
        {
            this.day = date.day;
            this.month = date.month;
            this.year = date.year;
        }

        //kiem tra xem doi tuong hien tai co truoc tham so truyen vao hay dung tra ve true, sai tra ve false
        public Boolean before (MyDate anotherDate)
        {
            if (anotherDate.year < this.year)
                return false;
            if(anotherDate.year == this.year)
            {
                if (anotherDate.month < this.month)
                    return false;
                if (anotherDate.month == this.month)
                {
                    if (anotherDate.year < this.year)
                        return false;
                }
            }
            return true;
        }

        //kiem tra xem doi tuong hien tai co bang ngay tham so truyen vao hay khong, dung tra vef true sai tra ve false
        public Boolean compare(MyDate anotherDate)
        {
            if (anotherDate.year != this.year)
                return false;
            if (anotherDate.month != this.month) 
                return false;
            if (anotherDate.day != this.day)
                return false;
            return true;
        }

        //ham in ra thu ngay thang nam
        public String toString()
        {
            String a;
            String b;
            MyDate _1_1_2012 = new MyDate(1, 1, 2012);

            //tinh xem hom day la ngay thu bao nhieu cua nam
            int count1 = 0;
            switch (this.month)
            {
                case 1:
                    {
                        count1 = this.day;
                        break;
                    }

                case 2:
                    {
                        count1 = (31 + this.day);
                        break;
                    }

                case 3:
                    {
                        if ((this.year % 4) == 0)
                            count1 = (60 + this.day);
                        else count1 = (59 + this.day);
                        break;
                    }

                case 4:
                    {
                        if ((this.year % 4) == 0)
                            count1 = (91 + this.day);
                        else count1 = (90 + this.day);
                        break;
                    }

                case 5:
                    {
                        if ((this.year % 4) == 0)
                            count1 = (121 + this.day);
                        else count1 = (120 + this.day);
                        break;
                    }

                case 6:
                    {
                        if ((this.year % 4) == 0)
                            count1 = (152 + this.day);
                        else count1 = (151 + this.day);
                        break;
                    }

                case 7:
                    {
                        if ((this.year % 4) == 0)
                            count1 = (182 + this.day);
                        else count1 = (181 + this.day);
                        break;
                    }

                case 8:
                    {
                        if ((this.year % 4) == 0)
                            count1 = (213 + this.day);
                        else count1 = (212 + this.day);
                        break;
                    }

                case 9:
                    {
                        if ((this.year % 4) == 0)
                            count1 = (244 + this.day);
                        else count1 = (243 + this.day);
                        break;
                    }

                case 10:
                    {
                        if ((this.year % 4) == 0)
                            count1 = (274 + this.day);
                        else count1 = (273 + this.day);
                        break;
                    }
                case 11:
                    {
                        if ((this.year % 4) == 0)
                            count1 = (305 + this.day);
                        else count1 = (304 + this.day);
                        break;
                    }
                case 12:
                    {
                        if ((this.year % 4) == 0)
                            count1+= (335 + this.day);
                        else count1 = (334 + this.day);
                        break;
                    }
            }

            //so sanh xem ngay hom day la truoc hay sau ngay 1/1/2012, va cach ngay 1/1/2012 bao nhieu ngay tu do tinh ra thu hom day (1/1/2012 la chu nhat)
            int count2 = 0;
            if (this.year < 2012)
            {
                int temp = 2012 - this.year;
                count2 = temp * 365 + (temp / 4) - count1 + 1;
                temp = count2 % 7;
                switch (temp)
                {
                    case 0:
                        {
                            b = "chu nhat";
                            break;
                        }
                    case 1:
                        {
                            b = "thu 7";
                            break;
                        }
                    case 2:
                        {
                            b = "thu 6";
                            break;
                        }
                    case 3:
                        {
                            b = "thu 5";
                            break;
                        }
                    case 4:
                        {
                            b = "thu 4";
                            break;
                        }
                    case 5:
                        {
                            b = "thu 3";
                            break;
                        }
                    case 6:
                        {
                            b = "thu 2";
                            break;
                        }

                    default:
                        {
                            b = "khong";
                            break;
                        }
                }
            }
            else 
            {
                int temp = this.year - 2012;
                count2= temp * 365 + (temp / 4) + count1;
                temp = count2 % 7;
                switch (temp)
                {
                    case 0:
                        {
                            b = "chu nhat";
                            break;
                        }
                    case 6:
                        {
                            b = "thu 7";
                            break;
                        }
                    case 5:
                        {
                            b = "thu 6";
                            break;
                        }
                    case 4:
                        {
                            b = "thu 5";
                            break;
                        }
                    case 3:
                        {
                            b = "thu 4";
                            break;
                        }
                    case 2:
                        {
                            b = "thu 3";
                            break;
                        }
                    case 1:
                        {
                            b = "thu 2";
                            break;
                        }

                    default:
                        {
                            b = "khong thy";
                            break;
                        }
                }
            }

            return (a = b + ", " + Convert.ToString(this.day) + "/" + Convert.ToString(this.month) + "/" + Convert.ToString(this.year));
        }
        
    }
}
